import 'package:xindong/common/xindong_api_path.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/manager/xindong_yinsi_singleton.dart';
import 'package:xindong/models/xindong_noble_class.dart';
import 'package:xindong/models/xindong_recharge_class.dart';
import 'package:xindong/routes/user/noble/xindong_noble_ctr.dart';
import 'package:xindong/routes/user/wallet/xindong_recharge_ctr.dart';
import 'package:xindong/widgets/dialog/xindong_alert_message.dart';
import 'package:xindong/widgets/dialog/xindong_options_sheet.dart';
import 'package:url_launcher/url_launcher_string.dart';

class PaymentMethod {
  static const int wx = 5; // 微信
  static const int ant = 6; // 支付宝
}

class XinDongPaymentSingleton {
  Future<void> buyRecharge(XinDongRechargeClass model, int paymentMethod) async {
    final result = await XinDongAlertMessage.delayShow(
      title: '提示',
      content: "金币为虚拟商品，一经充值无法退款，请理性消费",
      button: '确认',
    );
    if (!result) return;

    showLoading();
    final object = await XinDongNetworking.instance.request(
      XinDongApi.rechargeBuy,
      method: XinDongHttpMethod.post,
      parameters: {'id': model.id, "paymethod": paymentMethod},
    );
    if (object.code == XinDongServerStatus.success) {
      final order_sn = object.data["order_sn"];
      final url = object.data["url"];
      launchUrlString(url,  mode: LaunchMode.externalApplication);
      // XinDongJumpManager.instance.jumpToWebYeMian(title: "充值", url: url, inApp: true);
    }
    hideLoading();
    return Future.value();
  }

  // 购买会员
  void buyNoble(XinDongNobleClass model) async {
    if (XinDongYinSiSingleton.to.agreement.agreeHuiYuan != true) {
      final result = await XinDongAlertMessage.delayShow(
        title: '提示',
        content: "温馨提示：平台所有会员、充值均为虚拟商品，一旦支付即代表阅读并同意协议，请理性消费。如有疑问请联系客服。",
        button: '确认',
      );
      if (!result) return;
      XinDongYinSiSingleton.to.changeHuiYuan();
    }

    final object = await XinDongNetworking.instance.request(
      XinDongApi.nobleReBuy,
      method: XinDongHttpMethod.post,
      parameters: {'id': model.id, "types": 1},
    );
    if (object.code == XinDongServerStatus.success) {
      // final object = await XinDongNetworking.instance.request(
      //   XinDongApi.noblePayment,
      //   method: XinDongHttpMethod.post,
      // );
      XinDongOptionsSheet.show(
        dataList: const ["微信支付", "支付宝"],
        onSelect: (index) async {
          showLoading();
          final object = await XinDongNetworking.instance.request(
            XinDongApi.nobleBuy,
            method: XinDongHttpMethod.post,
            parameters: {'id': model.id, "method": index == 0 ? PaymentMethod.wx : PaymentMethod.ant},
          );
          if (object.code == XinDongServerStatus.success) {
            final order_sn = object.data["order_sn"];
            final url = object.data["url"];
            launchUrlString(url,  mode: LaunchMode.externalApplication);
            // XinDongJumpManager.instance.jumpToWebYeMian(title: "会员", url: url);
          }
          hideLoading();
        },
      );
    } else {}
    return Future.value();
  }

  // 公共处理成功需要做的事情
  void commonSuccessfulHandler() async {
    // 提示购买完成
    // showToast(XinDongConfig.textPurchaseCompleted);
    // if (type == PayType.recharge) {
    //   await XinDongUserController.to.requestUserAccount();
    // } else if (type == PayType.flowCard) {
    //   try {
    //     if (XinDongGlobal.instance.isPromotion) {
    //       XinDongPromotionController.to.load();
    //     }
    //   } catch (e) {
    //     XinDongDebugPrint(e);
    //   }
    // } else {
    //   await XinDongUserController.to.requestVipList();
    XinDongUserController.to.requestUserInfo();
    XinDongRechargeCtr.to.requestGetList();
    XinDongNobleCtr.to.requestGetList();
    // }
  }

  // ======== 单例
  // 工厂方法构造函数
  factory XinDongPaymentSingleton() => _getInstance();

  // instance的getter方法，singletonManager.instance获取对象
  static XinDongPaymentSingleton get instance => _getInstance();

  // 静态变量_instance，存储唯一对象
  static XinDongPaymentSingleton? _instance;

  // 获取对象
  static XinDongPaymentSingleton _getInstance() {
    _instance ??= XinDongPaymentSingleton._internal();
    return _instance!;
  }

  // 私有的自定义命名式构造方法，通过它实现一个类;
  // 子类不能继承internal
  // 不是关键字，可定义其他名字
  XinDongPaymentSingleton._internal() {
    //初始化(设置一些默认的)...
  }
}
